package com.gy.def.controller;

import com.gy.def.dataobject.CourseTable;
import com.gy.def.dataobject.UserTable;
import com.gy.def.enums.ResultEnum;
import com.gy.def.exception.GySchoolException;
import com.gy.def.service.CourseService;
import com.gy.def.service.UserService;
import com.gy.def.utils.ResultVoUtil;
import com.gy.def.viewobject.PageMap;
import com.gy.def.viewobject.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

@CrossOrigin // 允许跨域
@Api(value = "CourseController", description = "课程模块")
@RestController
@RequestMapping("/course")
public class CourseController {

  @Autowired
  private CourseService courseService;

  @ApiOperation(value = "添加课程", notes = "添加课程")
  @ApiImplicitParams({
    @ApiImplicitParam(paramType = "query", name = "courseAddress", value = "课程地点", required = true, dataType = "string"),
    @ApiImplicitParam(paramType = "query", name = "courseName", value = "课程名", required = true, dataType = "string"),
    @ApiImplicitParam(paramType = "query", name = "coursePoint", value = "课程学分", required = true, dataType = "int"),
    @ApiImplicitParam(paramType = "query", name = "courseTeach", value = "授课教师", required = true, dataType = "string"),
    @ApiImplicitParam(paramType = "query", name = "courseTime", value = "课时", required = true, dataType = "string"),
  })
  @PostMapping("/addCourse")
  private ResultVo addCourse(@RequestParam("courseAddress") String courseAddress, @RequestParam("courseName") String courseName,
                             @RequestParam("coursePoint") int coursePoint, @RequestParam("courseTeach") String courseTeach,
                             @RequestParam("courseTime") String courseTime) {

    CourseTable cta = courseService.findByCourseNameAndIsDelete(courseName, 0);
    if (cta != null) {
      throw new GySchoolException(ResultEnum.ErrorCourseRepetition);
    }


    CourseTable ct = new CourseTable();
    ct.setCourseAddress(courseAddress);
    ct.setCourseName(courseName);
    ct.setCoursePoint(coursePoint);
    ct.setCourseTeach(courseTeach);
    ct.setCourseTime(courseTime);

    CourseTable result = courseService.save(ct);

    if (result == null) {
      throw new GySchoolException(ResultEnum.FAIL);
    }

    return ResultVoUtil.success();
  }

  @ApiOperation(value = "获取课程列表", notes = "获取课程列表")
  @ApiImplicitParams({
    @ApiImplicitParam(paramType = "query", name = "page", value = "第几页", required = true, dataType = "string"),
    @ApiImplicitParam(paramType = "query", name = "pageSize", value = "一页有多少个数据", required = true, dataType = "string"),
  })
  @PostMapping("/getCourseList")
  private ResultVo<PageMap<CourseTable>> getCourseList(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                                       @RequestParam(value = "pageSize", defaultValue = "0") Integer pageSize) {
    Page<CourseTable> mapList;
    PageRequest pageRequest = new PageRequest(page, pageSize);
    mapList = courseService.findByIsDelete(0, pageRequest);

    PageMap<CourseTable> pm = new PageMap<>();
    pm.setNumber(mapList.getNumber());
    pm.setResultList(mapList.getContent());
    pm.setSize(mapList.getSize());
    pm.setTotalElements(mapList.getTotalElements());
    pm.setTotalPages(mapList.getTotalPages());

    return ResultVoUtil.success(pm);
  }

  @ApiOperation(value = "删除课程", notes = "删除课程")
  @ApiImplicitParams({
    @ApiImplicitParam(paramType = "query", name = "courseId", value = "课程id", required = true, dataType = "string")
  })
  @PostMapping("/deleteCourse")
  private ResultVo deleteCourse(@RequestParam("courseId") String courseId) {
    CourseTable ct = courseService.findByCourseId(courseId);
    if (ct == null) {
      throw new GySchoolException(ResultEnum.ErrorCourseNull);
    }

    ct.setIsDelete(1);
    CourseTable result = courseService.save(ct);
    if (result == null) {
      throw new GySchoolException(ResultEnum.FAIL);
    }
    return ResultVoUtil.success();
  }

  @ApiOperation(value = "修改课程", notes = "修改课程")
  @ApiImplicitParams({
    @ApiImplicitParam(paramType = "query", name = "courseAddress", value = "课程地点", required = true, dataType = "string"),
    @ApiImplicitParam(paramType = "query", name = "courseName", value = "课程名", required = true, dataType = "string"),
    @ApiImplicitParam(paramType = "query", name = "coursePoint", value = "课程学分", required = true, dataType = "int"),
    @ApiImplicitParam(paramType = "query", name = "courseTeach", value = "授课教师", required = true, dataType = "string"),
    @ApiImplicitParam(paramType = "query", name = "courseTime", value = "课时", required = true, dataType = "string"),
    @ApiImplicitParam(paramType = "query", name = "courseId", value = "课程id", required = true, dataType = "string"),
  })
  @PostMapping("/modifyCourse")
  private ResultVo modifyCourse(@RequestParam("courseAddress") String courseAddress, @RequestParam("courseName") String courseName,
                                @RequestParam("coursePoint") int coursePoint, @RequestParam("courseId") String courseId,
                                @RequestParam("courseTeach") String courseTeach, @RequestParam("courseTime") String courseTime) {

    CourseTable cta = courseService.findByCourseNameAndIsDelete(courseName, 0);
    if (cta != null) {
      throw new GySchoolException(ResultEnum.ErrorCourseRepetition);
    }


    CourseTable ct = courseService.findByCourseId(courseId);
    if (ct == null) {
      throw new GySchoolException(ResultEnum.ErrorCourseNull);
    }

    ct.setCourseAddress(courseAddress);
    ct.setCourseName(courseName);
    ct.setCoursePoint(coursePoint);
    ct.setCourseTeach(courseTeach);
    ct.setCourseTime(courseTime);

    CourseTable result = courseService.save(ct);

    if (result == null) {
      throw new GySchoolException(ResultEnum.FAIL);
    }

    return ResultVoUtil.success();
  }

}
